Altering existing routes
Create src/Routing/RouteSubscriber.php in your module. It will hold the route subscriber class:
<?php namespace Drupal\mymodule\Routing;
use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
/** * {@inheritdoc} */
public function alterRoutes(RouteCollection $collection) { // Change path of mymodule.mypage to use a hyphen if ($route = $collection->get('mymodule.mypage')) {
$route->setPath('/my-page'); }
} }
For Drupal to recognize the subscriber, we need to describe it in the module’s services.yml le. In the base directory of your module, create a mymodule.services.yml le and add the following code:
services: mymodule.route_subscriber:
class: Drupal\mymodule\Routing\RouteSubscriber tags:
- { name: event_subscriber }